package com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.caf.db.dbaccess.DbParameter;
import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.caf.db.dbaccess.IDbParameter;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import com.inspur.edp.task.entity.TaskComment;
import com.inspur.edp.task.filter.TaskCommentFilter;
import com.inspur.edp.task.service.TaskCommentService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;

import java.util.ArrayList;
import java.util.List;

public class SbssSbtzSetBzxxBySpMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String devId;
	private String spId;
	public SbssSbtzSetBzxxBySpMgrAction(IBEManagerContext managerContext, String devId, String spId) {
		super(managerContext);
		this.devId = devId;
		this.spId = spId;
	}

	private String tzCfg = "com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.SbssSbtz";

	private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);
	@Override
	public  void execute() {
		IStandardLcp tzLcp = LcpUtil.getLcp(tzCfg);

		RpcClassHolder rpcClassHolder = SpringBeanUtils.getBean(RpcClassHolder.class);
		TaskCommentService taskCommentService = rpcClassHolder.getRpcClass(TaskCommentService.class);

		// 获取首位执行人,将首位执行人的班组信息取出
		TaskCommentFilter taskFilter = new TaskCommentFilter();
		taskFilter.setBizInstId(spId);
		List<TaskComment> taskCommentList = taskCommentService.getTaskCommentList(taskFilter);
		if ( taskCommentList.isEmpty()){
			return;
		}
		// 获取首位
		TaskComment taskComment = taskCommentList.get(0);
		if (taskComment.getId() == null){
			return;
		}
		String userId = taskComment.getOperatorId();

		// 查询班组信息
		String rySql = "select bzgl.id as id from SBSS_BZGL_BZGL bzgl join SBSS_BZGL_BZRYMX bzmx on bzgl.ID = bzmx.PARENTID where bzmx.RYID = :ryid ";
		List<IDbParameter> params = new ArrayList<>();
		params.add(new DbParameter("ryid", userId));
		bqlExecuter.getOptions().setOptionType(OptionType.ExecuteSql);
		List<DynamicResultRow> dynamicResultRows = bqlExecuter.executeSelectStatement(rySql, new ArrayList<>(), params.toArray(new IDbParameter[]{}));
		// 没有给该人员设置班组
		if (dynamicResultRows.isEmpty()){
			return;
		}

		String bzId = dynamicResultRows.get(0).get("id").toString();

		ModifyChangeDetail devChange = new ModifyChangeDetail(devId);
		AssociationInfo czbmInfo = new AssociationInfo();
		czbmInfo.setValue("czbm",bzId);
		devChange.setItem("czbm",czbmInfo);
		tzLcp.modify(devChange);
		tzLcp.save();
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
}